<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\model\Bill;
use app\common\model\Order;
use think\Exception;
use think\Log;

class Callback extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    /**
     * 诺诺发票开票回调
     */
    public function nuo()
    {
        try{
            $params = $_POST;
            $content = json_decode($params['content'],true);
            Log::info($params);
            if (empty($params))
                throw new Exception('无内容');
            $bill_info = Bill::get(['orderNo'=>$params['orderno']]);
            if (empty($bill_info))
                throw new Exception('发票记录不存在');
            if (in_array($bill_info->status,[2,22,24,3])){
                echo json_encode(['status'=>'0000','message'=>'同步成功']);
                exit();
            }else{
                if ($content['c_status'] == 1){ //开票完成
                    $bill_info->status = 2;//实际开票状态
                    $bill_info->client_status = 1;//客户端显示状态
                    $bill_info->pdfUrl = $content['c_url'];
                    $bill_info->imgUrls = $content['c_imgUrls'];
                    $bill_info->invoiceCode = $content['c_fpdm'];
                    $bill_info->invoiceNo = $content['c_fphm'];
                    $bill_info->reason = '';
                    $bill_info->save();
                }elseif ($content['c_status'] == 2){//开票失败
                    $bill_info->status = 22;
                    $bill_info->reason = $content['c_errorMessage'] ?? '';
                    $bill_info->save();
                }elseif ($content['c_status'] == 3){//开票成功签章失败
                    $bill_info->status = 24;
                    $bill_info->reason = $content['c_errorMessage'] ?? '';
                    $bill_info->save();
                }
                echo json_encode(['status'=>'0000','message'=>'同步成功']);
                exit();
            }
        }catch (Exception $e){
            Log::error($e->getMessage());
            exit($e->getMessage());
        }
    }

    /**
     * 诺诺发票红冲开票回调
     */
    public function red_nuo()
    {
        try{
            $params = $_POST;
            $content = json_decode($params['content'],true);
            Log::info($params);
            if (empty($params))
                throw new Exception('无内容');
            $bill_info = Bill::get(['invoiceSerialNum'=>$content['c_fpqqlsh']]);
            if (empty($bill_info))
                throw new Exception('发票记录不存在');
            if (in_array($bill_info->status,[2,22,24,3])){
                echo json_encode(['status'=>'0000','message'=>'同步成功']);
                exit();
            }else{
                if ($content['c_status'] == 1){ //开票完成
                    $bill_info->status = 2;//实际开票状态
                    $bill_info->client_status = 1;//客户端显示状态
                    $bill_info->pdfUrl = $content['c_url'];
                    $bill_info->imgUrls = $content['c_imgUrls'];
                    $bill_info->invoiceCode = $content['c_fpdm'];
                    $bill_info->invoiceNo = $content['c_fphm'];
                    $bill_info->reason = '';
                    $bill_info->save();
                    Bill::update(['client_status'=>2],['id'=>$bill_info->parent_id]);
                }elseif ($content['c_status'] == 2){//开票失败
                    $bill_info->status = 22;
                    $bill_info->reason = $content['c_errorMessage'] ?? '';
                    $bill_info->save();
                }elseif ($content['c_status'] == 3){//开票成功签章失败
                    $bill_info->status = 24;
                    $bill_info->reason = $content['c_errorMessage'] ?? '';
                    $bill_info->save();
                }
                echo json_encode(['status'=>'0000','message'=>'同步成功']);
                exit();
            }
        }catch (Exception $e){
            Log::error($e->getMessage());
            exit($e->getMessage());
        }
    }
}
